home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / libdump / svc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-24  |  1.7 KB  |  66 lines

  1. /*  svc.c  --  service functions  */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include "svc.h"
  8.  
  9. /* Takes a string of 1-byte length/data format, and make an ASCIIZ */
  10. char *makeasciiz(unsigned char *lstring)
  11. {
  12.   char *asciizstring;
  13.   unsigned char stringlength = *lstring++;
  14.  
  15.   if ((asciizstring = malloc((int) stringlength + 1)) == NULL)
  16.      return (NULL);
  17.  
  18.   strncpy(asciizstring, (signed char *) lstring, stringlength);
  19.   asciizstring[stringlength] = '\0';
  20.  
  21.   return (asciizstring);
  22. }
  23.  
  24.  
  25. /* Writes to the output stream. This function adds an exception-handling
  26.    layer to disk I/O. It handles abnormal program termination, and warnings
  27.    to both stderr and output. Three types of messages can be handled:
  28.      Message - simply printed to a file
  29.      Warning - print to a file AND stderr
  30.      Error   - same as Warning, but terminate with abnormal exit code
  31. */
  32. void output (MESSAGETYPE msgtype, FILE *stream, char *outputformat, ...)
  33. {
  34.   char outputbuffer[133];
  35.   va_list varargp;
  36.  
  37.   va_start(varargp, outputformat);
  38.   vsprintf(outputbuffer, outputformat, varargp);
  39.  
  40.   /* if this is (non-fatal) warning or (fatal) error, also send it to stderr */
  41.   if (msgtype != message)
  42.      fprintf(stderr, "\a%s", outputbuffer);
  43.  
  44.   /* regardless, attempt to send message to output file - exception check */
  45.   if (stream != NOFILE)
  46.      if ((size_t) fprintf(stream, outputbuffer) != strlen(outputbuffer))
  47.       {
  48.         fprintf(stderr, "\aDisk Write Failure!\n");
  49.         abort();
  50.       }
  51.  
  52.   /* if this was (fatal) error message, abort on the spot [and any other bodily function ...] */
  53.   if (msgtype == error)
  54.    {
  55.      flushall();
  56.      fcloseall();
  57.      abort();
  58.    }
  59.  
  60.   va_end(varargp);
  61.   return;
  62. }
  63.  
  64.  
  65.  
  66.